 /* This file is part of Stimer. Stimer - Simple reminder tool.
    Copyright (C) 2012  Klimov Max <cleemmi@gmail.com>

    Stimer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    */
#include "reminderview.h"
#include <QGridLayout>
#include <QDialog>
#include <QWidgetAction>
#include <QMenu>

ReminderView::ReminderView(QWidget *parent) :
    QDialog(parent)
{
    this->setWindowFlags(Qt::Popup|Qt::WindowStaysOnTopHint);
    this->hide();
    init(parent);
}


void ReminderView::init(QWidget *parent, Qt::Orientation orientation)
{
    menu        = new QMenu;
    QWidgetAction *wa = new QWidgetAction(menu);
    frame       = new QFrame(menu);
    wa->setDefaultWidget(frame);
    frame->setLineWidth(2);
    frame->setFrameStyle(QFrame::Panel|QFrame::Plain);
    QGridLayout *gl = new QGridLayout;
    frame->setLayout(gl);
    lseconds    = new QLabel;
    lminutes    = new QLabel;
    bset        = new QPushButton("Set");
    breturn     = new QPushButton("Return");
    minutes     = new QSlider(orientation);
    minutes->setToolTip("Minutes");
    minutes->setRange(0,60);
    minutes->setTickInterval(5);
    minutes->setTickPosition(orientation==Qt::Vertical?QSlider::TicksAbove:QSlider::TicksBelow);
    seconds     = new QSlider(orientation);
    seconds->setToolTip("Seconds");
    seconds->setRange(0,60);
    seconds->setTickInterval(5);
    seconds->setTickPosition(QSlider::TicksBelow);
    connect(minutes,SIGNAL(valueChanged(int)),this,SLOT(setMinutes(int)));
    connect(seconds,SIGNAL(valueChanged(int)),this,SLOT(setSeconds(int)));
    connect(bset,SIGNAL(clicked()),this,SLOT(pressSet()));
    connect(breturn,SIGNAL(clicked()),this,SLOT(pressReturn()));
    setMinutes(0);
    setSeconds(0);

    gl->addWidget(lminutes,0,0,Qt::AlignCenter);
    gl->addWidget(lseconds,0,1,Qt::AlignCenter);
    gl->addWidget(minutes,1,0,Qt::AlignCenter);
    gl->addWidget(seconds,1,1,Qt::AlignCenter);
    gl->addWidget(bset,2,0,Qt::AlignCenter);
    gl->addWidget(breturn,2,1,Qt::AlignCenter);

    menu->addAction(wa);
    frame->show();
}


void ReminderView::setMinutes(int value)
{
    minutes->setValue(value);
    lminutes->setText("Minutes: "+QString::number(value));
}

void ReminderView::setSeconds(int value)
{
    seconds->setValue(value);
    lseconds->setText("Seconds: "+QString::number(value));
}

void ReminderView::setTime(const QTime &time)
{
    setSeconds(time.second());
    setMinutes(time.minute());
    if (time==this->time) return;
    this->time = time;
    emit timeChanged(this->time);
}

void ReminderView::pressSet()
{
    setTime(QTime(time.hour(),minutes->value(),seconds->value(),time.msec()));
    menu->hide();
}

void ReminderView::pressReturn()
{
    setTime(time);
}

void ReminderView::showOnCursor()
{
    menu->popup(QCursor::pos());
}
